﻿using System;

namespace SimpleServiceBus.Bus.Pipeline
{
    public class PipelineEvents : IPipelineEvents
    {
        #region IPipelineEvents Members

        public event EventHandler<MessagePipelineEventArgs> PipelineInitialized;
        public event EventHandler<MessagePipelineStatusChangedEventArgs> PipelineStatusChanged;
        public event EventHandler<MessagePipelineEventArgs> ProcessingComponentStarted;
        public event EventHandler<MessagePipelineEventArgs> ProcessingComponentCompleted;
        public event EventHandler<MessagePipelineEventArgs> PipelineStarted;
        public event EventHandler<MessagePipelineEventArgs> PipelineCompleted;
        public event EventHandler<MessagePipelineErrorEventArgs> PipelineError;

        #endregion

        public virtual void OnPipelineStatusChanged(MessagePipelineStatusChangedEventArgs args)
        {
            EventHandler<MessagePipelineStatusChangedEventArgs> evt = PipelineStatusChanged;
            if (evt != null)
                evt(this, args);
        }

        public virtual void OnProcessingComponentStarted(MessagePipelineEventArgs args)
        {
            EventHandler<MessagePipelineEventArgs> evt = ProcessingComponentStarted;
            if (evt != null)
                evt(this, args);
        }

        public virtual void OnProcessingComponentCompleted(MessagePipelineEventArgs args)
        {
            EventHandler<MessagePipelineEventArgs> evt = ProcessingComponentCompleted;
            if (evt != null)
                evt(this, args);
        }

        public virtual void OnPipelineStarted(MessagePipelineEventArgs args)
        {
            EventHandler<MessagePipelineEventArgs> evt = PipelineStarted;
            if (evt != null)
                evt(this, args);
        }

        public virtual void OnPipelineCompleted(MessagePipelineEventArgs args)
        {
            EventHandler<MessagePipelineEventArgs> evt = PipelineCompleted;
            if (evt != null)
                evt(this, args);
        }

        public virtual bool OnPipelineError(MessagePipelineErrorEventArgs args)
        {
            EventHandler<MessagePipelineErrorEventArgs> evt = PipelineError;
            if (evt != null)
            {
                evt(this, args);
                return true;
            }
            return false;
        }

        public virtual void OnPipelineInitialized(MessagePipelineEventArgs args)
        {
            var evt = PipelineInitialized;
            if (evt != null)
                evt(this, args);
        }
    }
}